package com.kai.controller;

import com.kai.pojo.Comment;
import com.kai.pojo.Course;
import com.kai.pojo.User;
import com.kai.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 通过课程id查出课程的留言
     * @param courseId
     * @return
     */
    @GetMapping("/selectCommentByCourseId")
    @ResponseBody
    public List<Comment> selectCommentByCourseId(String courseId) {
        return commentService.selectCommentByCourseId(courseId);
    }

    /**
     *模糊查询分页查出留言
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectAllComment")
    @ResponseBody
    public Map<String, Object> selectAllComment(int pageNum, int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        int pageTotal = commentService.selectCountComment();
        List<Comment> commentList = commentService.selectAllComment((pageNum-1)*pageSize,pageSize);
        map.put("commentList",commentList);
        map.put("pageTotal",pageTotal);
        return map;
    }

    /*回复留言*/
    @PostMapping("/reply")
    @ResponseBody
    public int reply(@RequestBody Comment comment) {
        comment.setReplyDate(new Date());
        return commentService.reply(comment);
    }
    /*用户留言*/
    @PostMapping("/comment")
    @ResponseBody
    public int comment(String userId,
                       String courseId,
                       String commentContent) {
        Comment comment = new Comment();
        comment.setCommentDate(new Date());
        comment.setCommentContent(commentContent);
        return commentService.insertComment(comment, courseId, userId);
    }
}
